package com.firmboy.search.load;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class LoadProperties {
	
	public static String propertiesPath = "/properties/config.properties";
	
	/**
	 * 加载propertiesPath路径下的properties文件，通过的是BufferReader这个类
	 * @return
	 */
	public static Map<String,String> getProperties(){
		URL resource = LoadProperties.class.getResource(propertiesPath);
		String fileName = resource.getFile();
		HashMap<String,String> map = new HashMap<String,String>();
		BufferedReader reader = null;
		try {
			File file = new File(fileName);
			reader = new BufferedReader(new FileReader(file));
			String tempString = "";
			int line = 1;
			while((tempString = reader.readLine())!=null){
				System.err.println(line+"-"+tempString);
				line++;
				String[] split = tempString.split("=");
				if(split.length==2){
					map.put(split[0], split[1]);
				}
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return map;
	}
	
	/**
	 * 加载propertiesPath路径下的properties文件，通过的是BufferReader这个类
	 * @return
	 */
	public static Map<String,String> getProperties(String path){
		URL resource = LoadProperties.class.getResource(propertiesPath);
		String fileName = resource.getFile();
		HashMap<String,String> map = new HashMap<String,String>();
		BufferedReader reader = null;
		try {
			File file = new File(fileName);
			reader = new BufferedReader(new FileReader(file));
			String tempString = "";
			int line = 1;
			while((tempString = reader.readLine())!=null){
				System.err.println(line+"-"+tempString);
				line++;
				String[] split = tempString.split("=");
				if(split.length==2){
					map.put(split[0], split[1]);
				}
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return map;
	}
}
